package com.blue.controller.author;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.blue.common.annotation.ReplaceParam;
import com.blue.common.constant.CommonConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.utils.QueryWrapperUtils;
import org.springframework.web.bind.annotation.*;
import com.blue.common.utils.ConvertUtils;
import com.blue.po.author.DeptPO;
import com.blue.dto.author.DeptDTO;
import com.blue.service.author.IDeptService;
import com.blue.common.entity.ResponseResult;

import java.util.List;


/**
 * 部门表
 *
 * @author nanjie.chen
 * @email 15696471158@163.com
 * @date 2025-02-15
 */
@RestController
@RequestMapping("/author/dept")
@Tag(name = "部门表")
public class DeptController {
    @Autowired
    private IDeptService deptService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    @ReplaceParam("deptName")
    public ResponseResult<?> list(DeptDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        DeptPO deptPO = ConvertUtils.convert(DeptPO.class, requestDTO);
        MPJQueryWrapper<DeptPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(deptPO, "sort", CommonConstant.ASC);
        Page<DeptPO> page = new Page<>(pageNum, pageSize);
        if (StringUtils.isNotBlank(requestDTO.getDeptName()) || StringUtils.isNotBlank(requestDTO.getStatus())) {
            Page<DeptPO> pageResult = deptService.page(page, queryWrapper);
            return ResponseResult.okResult(pageResult);
        } else {
            queryWrapper.eq("t.parent_id", CommonConstant.ROOT);
            Page<DeptPO> pageResult = deptService.page(page, queryWrapper);
            deptService.treeData(pageResult.getRecords(), false);
            return ResponseResult.okResult(pageResult);
        }
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody DeptDTO requestDTO) {
        DeptPO deptPO = ConvertUtils.convert(DeptPO.class, requestDTO);
        if (StringUtils.isBlank(deptPO.getParentId())) {
            deptPO.setParentId(CommonConstant.ROOT);
        }
        deptService.save(deptPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    public ResponseResult<?> edit(@RequestBody DeptDTO requestDTO) {
        DeptPO deptPO = ConvertUtils.convert(DeptPO.class, requestDTO);
        deptService.updateById(deptPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        deptService.delete(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    public ResponseResult<?> queryById(@RequestBody DeptDTO requestDTO) {
        DeptPO deptPO = deptService.getById(requestDTO.getDeptId());
        return ResponseResult.okResult(deptPO);
    }

    @GetMapping("/treeData")
    @Operation(summary = "树形结构")
    public ResponseResult<?> treeData(@RequestParam(value = "deptName", required = false) String deptName) {
        LambdaQueryWrapper<DeptPO> queryWrapper = new LambdaQueryWrapper<DeptPO>()
                .eq(DeptPO::getStatus, CommonConstant.NORMAL)
                .orderByAsc(DeptPO::getSort);
        if (StringUtils.isNotBlank(deptName)) {
            queryWrapper.like(DeptPO::getDeptName, deptName);
            List<DeptPO> deptPOS = deptService.list(queryWrapper);
            return ResponseResult.okResult(deptPOS);
        }
        queryWrapper.eq(DeptPO::getParentId, CommonConstant.ROOT);
        List<DeptPO> deptPOS = deptService.list(queryWrapper);
        deptService.treeData(deptPOS, true);
        return ResponseResult.okResult(deptPOS);
    }
}
